package com.bc004346.travelus.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bc004346.travelus.FlightPagerActivity;
import com.bc004346.travelus.R;
import com.bc004346.travelus.adapter.FlightAdapter;
import com.bc004346.travelus.adapter.TripAdapter;
import com.bc004346.travelus.model.domain.DayTrip;
import com.bc004346.travelus.model.domain.Flight;
import com.bc004346.travelus.model.domain.Reservation;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.bc004346.travelus.fragment.FlightDetailsFragment.EXTRA_FLIGHTS;
import static com.bc004346.travelus.fragment.FlightDetailsFragment.EXTRA_FLIGHT_ID;
import static com.bc004346.travelus.utility.Utility.log;

/**
 * Created by Renats on 9/4/2014.
 * The new flight list
 */
public class TripListFragment extends ListFragment {

    //<editor-fold desc="Members">
    protected SwipeRefreshLayout mSwipeRefresh;
    private ArrayList<DayTrip> mDayTrips = new ArrayList<DayTrip>();
    private static final String KEY_TRIP = "com.bc004346.travelus.TripListFragment.Trip";
    //</editor-fold>

    //<editor-fold desc="Overrides">
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);

        if (savedInstanceState != null)
            //noinspection unchecked
            mDayTrips = (ArrayList<DayTrip>) savedInstanceState.getSerializable(KEY_TRIP);
        TripAdapter adapter = new TripAdapter(getActivity(), mDayTrips);
        setListAdapter(adapter);

        mSwipeRefresh = (SwipeRefreshLayout) getActivity().findViewById(R.id.activity_fragment_swipeRefresh);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        try {
            outState.putSerializable(KEY_TRIP, mDayTrips);
        } catch (Exception e) {
            log(e, getActivity());
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.item_selector, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        try {
            getListView().setOnScrollListener(new AbsListView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(AbsListView absListView, int i) {
                }

                @Override
                public void onScroll(AbsListView absListView,
                                     int firstVisibleItem,
                                     int visibleItemCount,
                                     int totalItemCount) {
                    if (mSwipeRefresh != null) {
                        if (firstVisibleItem == 0) mSwipeRefresh.setEnabled(true);
                        else mSwipeRefresh.setEnabled(false);
                    }
                }
            });
        } catch (Exception e) {
            log(e, getActivity());
        }
    }

    //</editor-fold>

    //<editor-fold desc="Private Methods">
    public void refresh(ArrayList<DayTrip> trips) {

        try {
            mDayTrips.clear();
            for(DayTrip trip : trips)
                mDayTrips.add(trip);
            final ListView lv;
            if (trips.size() > 0) {

                TripAdapter adapter = new TripAdapter(getActivity(), mDayTrips);
                setListAdapter(adapter);

                lv = getListView();
                if (lv != null) {
                    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
                    lv.setSelector(android.R.color.darker_gray);
                }

            } else {
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                        R.layout.list_flight_simple, new ArrayList<String>() {{
                    add("No Trips to display");
                }}
                );
                setListAdapter(adapter);
                lv = getListView();
                if (lv != null)
                    lv.setChoiceMode(ListView.CHOICE_MODE_NONE);

            }
        } catch (IllegalStateException e) {
            log(e.getMessage());
        } catch (Exception e) {
            log(e, getActivity());
        }
    }

    //</editor-fold>

}
